import axios from 'axios'
import { getToken } from "@/utils/token"
import router from '@/router';
import { layer } from "@layui/layer-vue"

const httpRequest = axios.create({
    baseURL: "http://localhost:81/api"
})

httpRequest.interceptors.request.use(function (config) {
    // 登录、请求验证码不需要携带token
    var currentUrl = config.url
    if(currentUrl != '/login' && currentUrl != '/captcha'){
        var token = getToken();
        config.headers.token = token
    }
    return config;
}, function (error) {
    return Promise.reject(error);
});

httpRequest.interceptors.response.use(function (response) {
    if(response.data.code == 4011){ //用户未登录
        layer.msg(response.data.message, { icon : 2, time: 1000})
        router.push("/login");
    }
    return response;
}, function (error) {
    return Promise.reject(error);
});



export default httpRequest